#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"

// 打印矩阵
void printMatrix(int** matrix, int size) {
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main() {
    int matrixSize = 4;
    int* matrixColSize = &matrixSize;

    // 初始化矩阵
    int** matrix = malloc(matrixSize * sizeof(int*));
    if (!matrix) {
        fprintf(stderr, "Failed to allocate memory for matrix.\n");
        return 1;
    }

    for (int i = 0; i < matrixSize; ++i) {
        matrix[i] = malloc(matrixSize * sizeof(int));
        if (!matrix[i]) {
            fprintf(stderr, "Failed to allocate memory for row %d.\n", i);
            // Free already allocated rows
            for (int j = 0; j < i; ++j) {
                free(matrix[j]);
            }
            free(matrix);
            return 1;
        }
    }

    // 填充矩阵
    for (int i = 0; i < matrixSize; ++i) {
        for (int j = 0; j < matrixSize; ++j) {
            matrix[i][j] = (i + 1) * 10 + (j + 1);
        }
    }

    // 输出原始矩阵
    printf("Original Matrix:\n");
    printMatrix(matrix, matrixSize);

    // 旋转矩阵
    rotate(matrix, matrixSize, matrixColSize);

    // 输出旋转后的矩阵
    printf("Rotated Matrix:\n");
    printMatrix(matrix, matrixSize);

    // 释放内存
    for (int i = 0; i < matrixSize; ++i) {
        free(matrix[i]);
    }
    free(matrix);

    return 0;
}
